# -*- coding: utf-8 -*-
from demo.demo_access_mgmt.models import Permission, RoleUsers, Role, RolePermissions
from demo.demo_access_mgmt.serializers import PermissionSerializer


class RoleHelper(object):

    @classmethod
    def get_role_and_permission(cls, user_id):
        # 通过user_id来查询其角色
        role_user_objs = RoleUsers.objects.filter(user_id=user_id)
        role_ids = [role_user_obj.role_id for role_user_obj in role_user_objs]
        role_objs = Role.objects.filter(id__in=role_ids)
        role_names = [role_obj.name for role_obj in role_objs]  # 角色名列表

        # 通过角色id来查询用户的权限
        role_permission_objs = RolePermissions.objects.filter(role_id__in=role_ids)
        permission_ids = list(set([role_permission_obj.permission_id for role_permission_obj in role_permission_objs]))
        return role_names, permission_ids

    @classmethod
    def add_role_names_to_user_list(cls, user_list):
        # 查询所有角色信息
        all_role_objs = Role.objects.all()
        all_role_dict = {role_obj.id: role_obj.name for role_obj in all_role_objs}

        user_ids = [item.get("user_id") for item in user_list]
        role_user_objs = RoleUsers.objects.filter(user_id__in=user_ids)
        role_user_dict = {role_user_obj.user_id: list() for role_user_obj in role_user_objs}
        for role_user_obj in role_user_objs:
            user_id = role_user_obj.user_id
            role_name_list = role_user_dict.get(user_id)
            role_name = all_role_dict.get(role_user_obj.role_id)

            if role_name not in role_name_list:
                role_name_list.append(role_name)

        for item in user_list:
            user_id = item.get("user_id")
            item["create_time"] = item["create_time"][:19].replace("T", " ")
            item["update_time"] = item["update_time"][:19].replace("T", " ")
            item["role_names"] = role_user_dict.get(user_id, list())

    @classmethod
    def get_role_permission(cls, permission_ids, is_admin=False):
        # 一次性全部把数据取出来
        permission_objs = Permission.objects.all()

        permission_datas = PermissionSerializer(permission_objs, many=True).data
        permission_datas_dict = {permission_data.get("id"): permission_data for permission_data in permission_datas}

        # 一级菜单
        first_menu_objs = list()
        first_ids = list()
        for permission_obj in permission_objs:
            if permission_obj.parent_id == 0:  # 父id为0的表示一级菜单
                permission_id = permission_obj.id
                first_data = permission_datas_dict.get(permission_id)
                first_menu_objs.append(first_data)  # 一级菜单的信息
                # 一级菜单的id
                first_ids.append(permission_id)

        # 二级菜单
        second_ids = list()
        second_parents_dict = {first_id: list() for first_id in first_ids}
        for permission_obj in permission_objs:
            parent_id = permission_obj.parent_id
            if parent_id in first_ids:  # 父id在一级菜单id列表里的为二级菜单
                permission_id = permission_obj.id
                second_data = permission_datas_dict.get(permission_id)
                second_list = second_parents_dict.get(parent_id)  # 根据父id获取二级list
                second_list.append(second_data)
                # 二级菜单的id
                second_ids.append(permission_id)

        for first_id in first_ids:  # 一级菜单存储二级菜单的数据
            first_data = permission_datas_dict.get(first_id)  # 取一级菜单的信息，根据一级父id
            first_data["child"] = second_parents_dict.get(first_id)  # 一级菜单存储二级菜单的数据

        # 三级菜单
        third_parents_dict = {second_id: list() for second_id in second_ids}
        for permission_obj in permission_objs:
            parent_id = permission_obj.parent_id
            if parent_id in second_ids:
                permission_id = permission_obj.id
                third_data = permission_datas_dict.get(permission_id)
                third_list = third_parents_dict.get(parent_id)
                if is_admin:
                    third_data["isTrue"] = True
                else:
                    third_data["isTrue"] = True if permission_id in permission_ids else False
                third_list.append(third_data)

        for second_id in second_ids:  # 二级菜单存储三级菜单的数据，根据二级父id
            second_data = permission_datas_dict.get(second_id)
            second_data["child"] = third_parents_dict.get(second_id)

        return first_menu_objs

    @classmethod
    def get_user_permission(cls, permission_ids, is_admin=False):
        # 一次性全部把数据取出来
        permission_objs = Permission.objects.all().order_by("parent_id").order_by("weight")

        permission_datas = PermissionSerializer(permission_objs, many=True).data
        permission_datas_dict = {permission_data.get("id"): permission_data for permission_data in permission_datas}

        # 一级菜单
        first_menu_objs = list()
        first_ids = list()
        for permission_obj in permission_objs:
            if permission_obj.parent_id == 0:  # 父id为0的表示一级菜单
                permission_id = permission_obj.id
                first_data = permission_datas_dict.get(permission_id)
                first_menu_objs.append(first_data)  # 一级菜单的信息
                # 一级菜单的id
                first_ids.append(permission_id)

        # 二级菜单
        second_ids = list()
        second_parents_dict = {first_id: list() for first_id in first_ids}
        for permission_obj in permission_objs:
            parent_id = permission_obj.parent_id
            if parent_id in first_ids:  # 父id在一级菜单id列表里的为二级菜单
                permission_id = permission_obj.id
                second_data = permission_datas_dict.get(permission_id)
                second_list = second_parents_dict.get(parent_id)  # 根据父id获取二级list
                second_list.append(second_data)
                # 二级菜单的id
                second_ids.append(permission_id)

        for first_id in first_ids:  # 一级菜单存储二级菜单的数据
            first_data = permission_datas_dict.get(first_id)  # 取一级菜单的信息，根据一级父id
            first_data["child"] = second_parents_dict.get(first_id)  # 一级菜单存储二级菜单的数据

        # 三级菜单
        third_parents_dict = {second_id: list() for second_id in second_ids}
        for permission_obj in permission_objs:
            parent_id = permission_obj.parent_id
            if parent_id in second_ids:
                permission_id = permission_obj.id
                third_data = permission_datas_dict.get(permission_id)
                third_list = third_parents_dict.get(parent_id)
                if is_admin:
                    third_list.append(third_data)
                else:
                    if permission_id in permission_ids:
                        third_list.append(third_data)

        for second_id in second_ids:  # 二级菜单存储三级菜单的数据，根据二级父id
            second_data = permission_datas_dict.get(second_id)
            second_data["child"] = third_parents_dict.get(second_id)

        return first_menu_objs
